package com.aipartner.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * 静态资源配置 - 最简化版本
 */
@Configuration
public class StaticResourceConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 获取项目根目录并配置uploads目录
        String projectRoot = System.getProperty("user.dir");
        Path uploadsDir = Paths.get(projectRoot, "uploads").toAbsolutePath().normalize();
        String uploadsLocation = "file:" + uploadsDir.toString().replace("\\", "/") + "/";

        // 配置 /uploads/** 静态资源映射到本地 uploads 目录
        registry.addResourceHandler("/uploads/**")
                .addResourceLocations(uploadsLocation);
        System.out.println("静态资源配置: /uploads/** -> " + uploadsLocation);

        // 同时映射 /images/** 到两处：classpath 和小程序静态资源目录，解决默认头像等资源
        Path wxappImagesDir = Paths.get(projectRoot).getParent().resolve("wxapp").resolve("images").toAbsolutePath().normalize();
        String wxappImagesLocation = "file:" + wxappImagesDir.toString().replace("\\", "/") + "/";
        registry.addResourceHandler("/images/**")
                .addResourceLocations("classpath:/static/images/", wxappImagesLocation);
        System.out.println("静态资源配置: /images/** -> classpath:/static/images/ , " + wxappImagesLocation);

        // 配置 /icons/** 静态资源映射到 classpath:/static/icons/
        registry.addResourceHandler("/icons/**")
                .addResourceLocations("classpath:/static/icons/");
        System.out.println("静态资源配置: /icons/** -> classpath:/static/icons/");

        // 兼容旧路径：/illustrate-images/** 直接映射到 uploads/illustrate-images/
        String illustrateLocation = uploadsLocation + "illustrate-images/";
        registry.addResourceHandler("/illustrate-images/**")
                .addResourceLocations(illustrateLocation);
        System.out.println("静态资源配置: /illustrate-images/** -> " + illustrateLocation);
        System.out.println("=== 静态资源配置完成 ===");
        
        // 打印调试信息
        System.out.println("当前工作目录: " + projectRoot);
        System.out.println("uploads目录路径: " + uploadsDir.toString());
        System.out.println("uploads映射URL: " + uploadsLocation);
    }
}